<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            height: 10000px;
        }

        .box {
            width: 50px;
            height: 50px;
            text-align: center;
            line-height: 50px;
            position: fixed;
            right: 20px;
            bottom: 20px;
            background: red;
            opacity: 0;
            transition: 0.5s;
        }
    </style>
</head>

<body>
    <div id="box" class="box">置顶</div>
    <script>
        var box = document.getElementById("box")
        var scrollTop
        window.onscroll = function(){
            scrollTop = document.documentElement.scrollTop || document.body.scrollTop
            if(scrollTop > window.innerHeight){
                box.style.opacity = 1
            }else{
                box.style.opacity = 0
            }
            
        }
        box.onclick = function(){
            
            if(scrollTop >= 0){
                var timer = setInterval(function(){
                    document.documentElement.scrollTop -= 100
                    // document.body.scrollTop -= 10
                },1)
            }else{
                clearInterval(timer)
            }
        }
        window.scroll = function(){

        }
    </script>
</body>

</html>